/*
 * 1、该BSDViewSdkDemo工程是配合北京东方联星科技有限公司北斗助手SDK使用的Demo工程。
 * 2、该工程仅供参考，提供SDK使用方法，不具备产品特性，如需技术支持请联系北京东方联星科技有限公司。
 * 3、该工程需要客户修改相关部分，具体有北斗伴侣蓝牙MAC地址、差分服务器地址、端口等，
 *    请在MainActivity.java中搜索“需要修改”找出具体位置，根据注释修改。
 * */

package library.sdk.demo.activity.module.m1;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.mapbox.mapboxsdk.geometry.LatLng;

import library.sdk.demo.parent.AbsMapActivity;
import mapbox.MapUtils;
import rtk.BluetoothConnectActivity;
import rtk.RtkData;
import rtk.RtkManager;


public class M1Activity extends AbsMapActivity {

    private boolean isRtkServer = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RtkManager.getInstance().init(this);
        mConn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mConn.getText().equals("连接")) {
                    Intent intent = new Intent();
                    intent.setClass(getActivity(), BluetoothConnectActivity.class);
                    startActivityForResult(intent, 0x0101);
                } else if (mConn.getText().equals("断开")) {
                    RtkManager.getInstance().disconnect();
                }
            }
        });

        // 电量和蓝牙信号
        if (RtkManager.getInstance().isBleConnected())
            powerAndRssi();
    }

    private void powerAndRssi() {
        RtkManager.getInstance().addRtkListener(new RtkManager.RtkListener() {
            @Override
            public void onBleConnectionState(Boolean connected) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (connected) {
                            mConn.setText("断开");
                        } else {
                            mConn.setText("连接");
                        }
                    }
                });
            }

            @Override
            public void onRtkDataSchedule(RtkData data) {
                if (data != null) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            LatLng latLng = MapUtils.getLatLonFromWGS84(data.getLatitude(), data.getLongitude());
                            data.setLatitude(latLng.getLatitude());
                            data.setLongitude(latLng.getLongitude());
                            LatLng projection = MapUtils.lonLat2Mercator(latLng);
                            mTextViewInfo.setText("信号 = " + data.getBleRssi() + " , 电量 = " + data.getDevicePower() + "%\n"
                                    + "纬度 = " + data.getLongitude() + "\t" + projection.getLongitude() + "\n"
                                    + "精度 = " + data.getLatitude() + "\t" + projection.getLatitude() + "\n"
                                    + "速度 = " + data.getSpeed() + "\n"
                                    + "方向 = " + data.getBearing() + "\n"
                                    + "模式 = " + data.getStringMode() + " , 卫星 = " + data.getUStates() + "\n"
                                    + "精度 = " + data.getAccuracy() + "\n"
                                    + "RTCM :" + (RtkManager.getInstance().isRtcmConnected() ? "已连接" : "未连接"));
                            addMarker(data);
                        }
                    });
                }
            }
        });
        RtkManager.getInstance().setRtkService(
                "rtk.ntrip.qxwz.com", // 差分服务器IP地址，需要修改
                8002, // 差分服务器端口号，需要修改
                "RTCM32_GGB", // 差分服务器接入点，需要修改
//                "bjghrs02", // 差分服务器用户名，需要修改
//                "731276a" // 差分服务器密码，需要修改
                "eepeege001",
                "d81e0f1"
        );
        RtkManager.getInstance().startSchedule(1000);
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void finish() {
        recycle();
        super.finish();
    }

    protected void recycle() {
        try {
            RtkManager.getInstance().disconnect();
        } catch (Exception e) {

        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x0101) {
            if (resultCode == RESULT_OK) {
                mConn.setText("断开");
                powerAndRssi();
            }
        }
    }

}
